SPL Discriminator
This library allows for easy management of 8-byte discriminators.
The ArrayDiscriminator
Struct
With this crate, you can leverage the ArrayDiscriminator
type to manage an 8-byte discriminator for generic purposes.
let my_discriminator = new;
The new(..)
function is also a constant function, so you can use ArrayDiscriminator
in constants as well.
const MY_DISCRIMINATOR: ArrayDiscriminator = new;
The ArrayDiscriminator
struct also offers another constant function as_slice(&self)
, so you can use as_slice()
in constants as well.
const MY_DISCRIMINATOR_SLICE: & = MY_DISCRIMINATOR.as_slice;
The SplDiscriminate
Trait
A trait, SplDiscriminate
is also available, which will give you the ArrayDiscriminator
constant type and also a slice representation of the discriminator. This can be particularly handy with match statements.
/// A trait for managing 8-byte discriminators in a slab of bytes
The SplDiscriminate
Derive Macro
The SplDiscriminate
derive macro is a particularly useful tool for those who wish to derive their 8-byte discriminator from a particular string literal. Typically, you would have to run a hash function against the string literal, then copy the first 8 bytes, and then hard-code those bytes into a statement like the one above.
Instead, you can simply annotate a struct or enum with SplDiscriminate
and provide a namespace via the discriminator_namespace
attribute, and the macro will automatically derive the 8-byte discriminator for you!
// Implements `SplDiscriminate` for your struct/enum using your declared string literal namespace
let my_discriminator: ArrayDiscriminator = SPL_DISCRIMINATOR;
let my_discriminator_slice: & = SPL_DISCRIMINATOR_SLICE;
Note: the 8-byte discriminator derived using the macro is always the first 8 bytes of the resulting hashed bytes.